package cn.itcast.bos.web.action.base;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

import javax.annotation.Resource;

import cn.itcast.bos.service.IStaffService;
import cn.itcast.bos.service.IUserService;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

/**
 * 抽取表现层
 * @author zhaoqx
 *
 */
public class BaseAction<T> extends ActionSupport implements ModelDriven<T> {
	//声明模型对象
	protected T model;
	public T getModel() {
		return model;
	}
	
	@Resource
	protected IUserService userService;
	@Resource
	protected IStaffService staffService;
	//在构造方法中动态获得实体的类型，并通过反射实例化模型对象
	public BaseAction() {
		//获得父类（BaseAction<T>）类型
		ParameterizedType genericSuperclass = (ParameterizedType) this
				.getClass().getGenericSuperclass();
		//获得父类上的泛型数组
		Type[] actualTypeArguments = genericSuperclass.getActualTypeArguments();
		//实体类型
		Class<T> domainClass = (Class<T>) actualTypeArguments[0];
		try {
			model = domainClass.newInstance();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
	}
}
